import request from '@/api/request'
import type { User } from '@/services/account/account.types'

export default class UserService {
  async getUserProfile(userId: string): Promise<User> {
    const response = await request.get(`/api/users/${userId}`)
    return response.data
  }

  async followUser(userId: string): Promise<void> {
    await request.post(`/api/users/${userId}/follow`)
  }

  async unfollowUser(userId: string): Promise<void> {
    await request.delete(`/api/users/${userId}/follow`)
  }

  async getFollowers(
    userId: string,
    page = 1,
    pageSize = 20,
  ): Promise<{ items: User[]; total: number }> {
    const response = await request.get(`/api/users/${userId}/followers`, {
      params: { page, pageSize },
    })
    return response.data
  }

  async getFollowing(
    userId: string,
    page = 1,
    pageSize = 20,
  ): Promise<{ items: User[]; total: number }> {
    const response = await request.get(`/api/users/${userId}/following`, {
      params: { page, pageSize },
    })
    return response.data
  }

  async getFriends(page = 1, pageSize = 20): Promise<{ items: User[]; total: number }> {
    const response = await request.get('/api/users/friends', {
      params: { page, pageSize },
    })
    return response.data
  }

  async searchUsers(
    keyword: string,
    page = 1,
    pageSize = 20,
  ): Promise<{ items: User[]; total: number }> {
    const response = await request.get('/api/users/search', {
      params: { keyword, page, pageSize },
    })
    return response.data
  }

  async updateAvatar(file: File): Promise<string> {
    const formData = new FormData()
    formData.append('avatar', file)
    const response = await request.post('/api/users/avatar', formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    })
    return response.data
  }
}
